package com.scott.crm.role.controller;

import com.scott.base.message.DataRes;
import com.scott.base.message.DataUtil;
import com.scott.crm.role.bean.RoleBean;
import com.scott.crm.role.bean.UserRoleBean;
import com.scott.crm.role.service.RoleService;
import com.scott.crm.role.service.UserRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 王伟雄
 * @create 2021/11/29 16:33
 */
@CrossOrigin
@RestController
@Slf4j
public class UserRoleController {

    @Autowired
    private UserRoleService service;

    @Autowired
    private RoleService roleService;

    @RequestMapping(value = "/userRole/add",method = RequestMethod.POST)
    public DataRes add(@RequestBody UserRoleBean userRoleBean) throws Exception{

        if(userRoleBean == null){
            return null;
        }

        log.info(userRoleBean.toString());
        int counter = service.add(userRoleBean);
        if(counter ==1 ){
            return DataUtil.success(userRoleBean);
        }else{
            return DataUtil.errors();
        }
    }

    @RequestMapping(value = "/userRole/delete",method = RequestMethod.POST)
    public DataRes delete(@RequestBody List<Integer> ids) throws Exception{

        if(ids == null){
            return null;
        }

        log.info(ids.toString());
        int counter = service.delete(ids);
        if(counter >0 ){
            return DataUtil.success(ids);
        }else{
            return DataUtil.errors();
        }
    }

    @RequestMapping(value = "/userRole/findAll",method = RequestMethod.GET)
    public DataRes findAll(@RequestParam(value="pageNumber",defaultValue = "1") Integer pageNumber,
                           @RequestParam(value="pageSize",defaultValue = "5") Integer pageSize) throws Exception{
        log.info(String.valueOf(pageNumber));
        log.info(String.valueOf(pageSize));
        if(pageNumber<1){
            pageNumber=1;
        }
        Integer begin=(pageNumber-1)*pageSize;
        Integer end =pageSize;

        List<UserRoleBean> userRoles = service.findAll(begin,end);

        Map<String,Object> rs= new HashMap<>();
        rs.put("userRoleList",userRoles);

        //pageNumber 第几页
        rs.put("pageNumber",pageNumber);
        //pageSize   1页多少条
        rs.put("pageSize",pageSize);

        List<UserRoleBean> userRoleBeans = service.findAll();
        List<RoleBean> allRoles = roleService.findAllRoles();
        rs.put("roleList",allRoles);
        if(!CollectionUtils.isEmpty(userRoleBeans)){
            Integer totalCounter = userRoleBeans.size();
            //totalCounter 一共多少条记录
            rs.put("totalCounter",totalCounter);

            //totalPage 共多少页
            Integer totalPage = totalCounter%pageSize==0?totalCounter/pageSize:totalCounter/pageSize+1;
            rs.put("totalPage",totalPage);

        }
        log.info("roleList="+userRoles.toString());
        return DataUtil.success(rs);
    }

    @RequestMapping(value = "/userRole/findAllLimitByRoleName",method = RequestMethod.POST)
    public DataRes findAllLimitByRoleName(@RequestBody UserRoleBean userRoleBean) throws Exception{
        Integer pageNumber = userRoleBean.getPageNumber();
        Integer pageSize = userRoleBean.getPageSize();

        log.info(String.valueOf(pageNumber));
        log.info(String.valueOf(pageSize));
        if(pageNumber<1){
            pageNumber=1;
        }
        Integer begin=(pageNumber-1)*pageSize;
        Integer end =pageSize;

        List<UserRoleBean> userRoles = service.findAllLimitByRoleName(begin,end,userRoleBean);

        Map<String,Object> rs= new HashMap<>();
        rs.put("userRoleList",userRoles);

        //pageNumber 第几页
        rs.put("pageNumber",pageNumber);
        //pageSize   1页多少条
        rs.put("pageSize",pageSize);

        List<UserRoleBean> userRoleBeans = service.findAllByRoleName(userRoleBean);
        List<RoleBean> allRoles = roleService.findAllRoles();
        rs.put("roleList",allRoles);
        if(!CollectionUtils.isEmpty(userRoleBeans)){
            Integer totalCounter = userRoleBeans.size();
            //totalCounter 一共多少条记录
            rs.put("totalCounter",totalCounter);

            //totalPage 共多少页
            Integer totalPage = totalCounter%pageSize==0?totalCounter/pageSize:totalCounter/pageSize+1;
            rs.put("totalPage",totalPage);

        }
        log.info("roleList="+userRoles.toString());
        return DataUtil.success(rs);
    }


}
